package com.zhao.dota.security.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhao.dota.struct.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Locale;

abstract class LoginHandler {

    @Resource
    private ObjectMapper objectMapper;

    <T> void writeResult(HttpServletResponse response, Result<T> data, Integer status) throws IOException {
        String json = objectMapper.writeValueAsString(data);
        response.setContentType("application/json");
        response.setLocale(Locale.CHINA);
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(json);
        if (status != null) {
            response.setStatus(status);
        }
    }

}
